<%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2024/12/10
  Time: 11:44
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String CONTENT = null;
    String TITLE = null;
    String USERID = null;
    String TEXT = null;
    String PICTURE = null;
    String TIEZIID = null;
    ResultSet rs = null;
    ResultSet rs2 = null;
    Connection conn = null;
    PreparedStatement pstmt = null;
    PreparedStatement pstmt2;

    HttpSession session1 = request.getSession();
    User user1 = (User) session1.getAttribute("user");

    // 数据库连接信息
    String url = "jdbc:mysql://localhost:3306/db1";
    String user = "root";
    String password = "123456";

    try {
        if (user1 != null) {
//            System.out.println(user1.getUserId());
        }

        // 加载驱动
        Class.forName("com.mysql.jdbc.Driver");

        // 建立连接
        conn = DriverManager.getConnection(url, user, password);
//        // 获取当前连接端口号
//        Statement stmt = conn.createStatement();
//        ResultSet rs1 = stmt.executeQuery("SELECT HOST FROM information_schema.PROCESSLIST WHERE ID = CONNECTION_ID();");
//
//        String hostPort = "未知端口";
//        if (rs1.next()) {
//            hostPort = rs1.getString("HOST");
//            System.out.println(hostPort);
//        }

        // 创建SQL语句
        String sql = "SELECT * FROM upload";
        pstmt = conn.prepareStatement(sql);
        rs = pstmt.executeQuery();

        String ti = "bx-like";
        String ts = "bx-star";
        String tc = "bx-coin";
        out.print("<div class='home'>");
        while (rs.next()) {

            // 获取查询结果
            TITLE = rs.getString("TITLE");
            CONTENT = rs.getString("CONTENT");
            USERID = rs.getString("USERID");
            TEXT = rs.getString("TEXT");
            PICTURE = rs.getString("PICTURE");
            TIEZIID = rs.getString("TIEZIID");

            UserDao userDao = new UserDaoimpl();
            UserName username = userDao.findByUserId(Integer.parseInt(USERID));



            if (user1 != null) {
                String sql2 = "CALL proc_tiezibyuserid(?)";
                pstmt2 = conn.prepareStatement(sql2);
                pstmt2.setInt(1, user1.getUserId());
                rs2 = pstmt2.executeQuery();
                while (rs2.next()) {
                    if (rs2.getString("TIEZIID").equals(TIEZIID)) {
                        if (rs2.getString("DIANZAN").equals("0")) {
                            ti = "bx-like";
                        } else {
                            ti = "bxs-like";
                        }
                        if (rs2.getString("SHOUCANG").equals("0")) {
                            ts = "bx-star";
                        } else {
                            ts = "bxs-star";
                        }
                        if (rs2.getString("TOUBI").equals("0")) {
                            tc = "bx-coin";
                        } else {
                            tc = "bxs-coin";
                        }
                    }
                }
            }

            out.print("<div class='CONTENT'>");
            out.print("<span style='border-radius: 1px;'>" + TIEZIID+"</span>");
            out.print("<span style='border-radius: 1px;padding-left: 5px'>" + username.getUsername()+"</span>");
            out.print("<h1>" + TITLE + "</h1>");
            out.print("<div class='TEXT'>");
            out.print(TEXT);
            out.print("</div>");
            out.print(PICTURE);
            out.print("<div class='sbox'>");
            out.print("<i class='bx " + ti + "'></i>");
            out.print("<i class='bx " + ts + "'></i>");
            out.print("<i class='bx " + tc + "'></i>");
            out.print("</div>");
            out.print("</div>");

            ti = "bx-like";
            ts = "bx-star";
            tc = "bx-coin";

        }
        out.print("</div>");
        out.print("<div id='popup'>");
        out.print("</div>");


//        // 执行SQL语句
//        pstmt.executeUpdate();

        // 关闭连接
        pstmt.close();
        rs.close();
        conn.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
%>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport"
          content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
    <title></title>
    <link href='https://unpkg.com/boxicons@2.1.4/css/boxicons.min.css' rel='stylesheet'>
    <link rel="stylesheet" href="./css/手机主页面.css">
    <link rel="stylesheet" href="./css/dropload.css">
    <style>
    </style>
</head>
<%@ page import="java.sql.*" %>
<%@ page import="com.example.entity.User" %>
<%@ page import="com.example.dao.UserDao" %>
<%@ page import="com.example.dao.impl.UserDaoimpl" %>
<%@ page import="com.example.entity.UserName" %>
<%@ page import="java.util.regex.Pattern" %>
<%@ page import="java.util.regex.Matcher" %>
<body>
</body>
<script src="./js/zepto.min.js"></script>
<script src="./js/dropload.min.js"></script>
<%--<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>--%>
<script>

        var dropload = $('.home').dropload({
            scrollArea : window,
            autoLoad: false, // 禁止自动加载
            domUp: {
                domClass: 'dropload-up',
                domRefresh: '<div class="dropload-refresh">↓下拉刷新</div>',
                domUpdate: '<div class="dropload-update">↑释放更新</div>',
                domLoad: '<div class="dropload-load"><span class="loading"></span>加载中...</div>'
            },
            domDown: {
                domClass: 'dropload-down',
                domRefresh: '<div class="dropload-refresh">↑上拉加载更多</div>',
                domLoad: '<div class="dropload-load"><span class="loading"></span>加载中...</div>',
                domNoData: '<div class="dropload-noData">暂无数据</div>'
            },

            loadDownFn: function (me) {
                $.ajax({
                    type: 'GET',
                    success: function () {
                        setTimeout(function () {
                            me.resetload();
                        }, 2000)
                    },
                    error: function (xhr, type) {
                        alert('Ajax error!');
                        // 即使加载出错，也得重置
                        me.resetload();
                    },
                })
            },
            loadUpFn: function (me) {
                $.ajax({
                    type: 'GET',
                    success: function () {
                        setTimeout(function () {
                            me.resetload();
                        }, 2000)
                    },
                    error: function (xhr, type) {
                        alert('Ajax error!');
                        // 即使加载出错，也得重置
                        me.resetload();
                    }
                })
            },
        });


    // var ilike = document.querySelector("#ilike")
    // var popup = document.getElementById('popup')
    // ilike.addEventListener("click", function() {
    // 	isClick = ilike.classList
    //
    // 	ilike.classList = isClick == 'bx bx-like' ?
    // 			'bx bxs-like' :
    // 			'bx bx-like';
    // 	ilike.classList == 'bx bxs-like' ? popup.textContent = "点赞收到!" : popup.textContent = "取消点赞!"
    // 	// 显示弹出窗口
    // 	popup.style.display = 'block';
    //
    // 	// 可选：设置一段时间后自动关闭弹出窗口
    // 	setTimeout(function() {
    // 		popup.style.display = 'none';
    // 	}, 1200); // 1200毫秒后关闭，即1.2秒
    // });
    //
    //
    // var istar = document.querySelector("#istar")
    // istar.addEventListener("click", function() {
    // 	isClick = istar.classList
    // 	istar.classList = isClick == 'bx bx-star' ?
    // 			'bx bxs-star' :
    // 			'bx bx-star';
    // 	istar.classList == 'bx bxs-star' ? popup.textContent = "收藏成功!" : popup.textContent = "取消收藏!"
    // 	// 显示弹出窗口
    // 	popup.style.display = 'block';
    //
    // 	// 可选：设置一段时间后自动关闭弹出窗口
    // 	setTimeout(function() {
    // 		popup.style.display = 'none';
    // 	}, 1200); // 1200毫秒后关闭，即1.2秒
    // })
    //
    // var icoin = document.querySelector("#icoin")
    // icoin.addEventListener("click", function() {
    // 	isClick = icoin.classList
    // 	icoin.classList = isClick == 'bx bx-coin' ?
    // 			'bx bxs-coin' :
    // 			'bx bx-coin';
    // 	icoin.classList == 'bx bxs-coin' ? popup.textContent = "投币成功!" : popup.textContent = "取消投币!"
    // 	// 显示弹出窗口
    // 	popup.style.display = 'block';
    //
    // 	// 可选：设置一段时间后自动关闭弹出窗口
    // 	setTimeout(function() {
    // 		popup.style.display = 'none';
    // 	}, 1200); // 1200毫秒后关闭，即1.2秒
    // })
    // 获取所有bx-like类的图标
    var likeIcons = document.querySelectorAll('i[class$="-like"]');
    likeIcons.forEach(function (icon) {
        icon.addEventListener("click", function () {
            var span = this.parentElement.parentElement.querySelector('span');
            var CheckLike = icon.classList.contains("bx-like");
            var CheckStar = icon.parentElement.querySelector('i[class$="-star"]').classList.contains("bxs-star");
            var CheckCoin = icon.parentElement.querySelector('i[class$="-coin"]').classList.contains("bxs-coin");
            console.log(CheckCoin)
            if (CheckLike) {
                icon.classList = 'bx bxs-like'
            } else {
                icon.classList = 'bx bx-like'
            }
            var popup = document.getElementById('popup');
            popup.textContent = CheckLike ? "点赞收到!" : "取消点赞!";
            popup.style.display = 'block';
            setTimeout(function () {
                popup.style.display = 'none';
            }, 1200);

            // 发送AJAX请求到服务器
            $.ajax({
                type: "POST",
                url: "sendtotiezi.jsp", // 服务器端处理页面
                data: {
                    CheckLike: CheckLike,
                    CheckStar: CheckStar,
                    CheckCoin: CheckCoin,
                    TIEZIID: span.textContent,
                    USERID:<%= user1 != null ? user1.getUserId() : "NULL" %>,
                    FLAG: "like",
                },
            });
        });

    });

    // 获取所有bx-star类的图标
    var starIcons = document.querySelectorAll('i[class$="-star"]');
    starIcons.forEach(function (icon) {
        icon.addEventListener("click", function () {
            var span = this.parentElement.parentElement.querySelector('span');
            var CheckLike = icon.parentElement.querySelector('i[class$="-like"]').classList.contains("bxs-like");
            var CheckStar = icon.classList.contains("bx-star");
            var CheckCoin = icon.parentElement.querySelector('i[class$="-coin"]').classList.contains("bxs-coin");
            if (CheckStar) {
                icon.classList = 'bx bxs-star'
            } else {
                icon.classList = 'bx bx-star'
            }
            var popup = document.getElementById('popup');
            popup.textContent = CheckStar ? "收藏成功!" : "取消收藏!";
            popup.style.display = 'block';
            setTimeout(function () {
                popup.style.display = 'none';
            }, 1200);

            // 发送AJAX请求到服务器
            $.ajax({
                type: "POST",
                url: "sendtotiezi.jsp", // 服务器端处理页面
                data: {
                    CheckLike: CheckLike,
                    CheckStar: CheckStar,
                    CheckCoin: CheckCoin,
                    TIEZIID: span.textContent,
                    USERID:<%= user1 != null ? user1.getUserId() : "NULL" %>,
                    FLAG: "star",
                },
            });
        });
    });

    // 获取所有bx-coin类的图标
    var coinIcons = document.querySelectorAll('i[class$="-coin"]');
    coinIcons.forEach(function (icon) {
        icon.addEventListener("click", function () {
            var span = this.parentElement.parentElement.querySelector('span');
            var CheckLike = icon.parentElement.querySelector('i[class$="-like"]').classList.contains("bxs-like");
            var CheckStar = icon.parentElement.querySelector('i[class$="-star"]').classList.contains("bxs-star");
            var CheckCoin = icon.classList.contains("bx-coin");
            if (CheckCoin) {
                icon.classList = 'bx bxs-coin'
            } else {
                icon.classList = 'bx bx-coin'
            }
            var popup = document.getElementById('popup');
            popup.textContent = CheckCoin ? "投币成功!" : "取消投币!";
            popup.style.display = 'block';
            setTimeout(function () {
                popup.style.display = 'none';
            }, 1200);

            // 发送AJAX请求到服务器
            $.ajax({
                type: "POST",
                url: "sendtotiezi.jsp", // 服务器端处理页面
                data: {
                    CheckLike: CheckLike,
                    CheckStar: CheckStar,
                    CheckCoin: CheckCoin,
                    TIEZIID: span.textContent,
                    USERID:<%= user1 != null ? user1.getUserId() : "NULL" %>,
                    FLAG: "coin",
                },
            });
        });
    });
</script>
</html>
